package org.leetcode.easy.leetcode20;

import org.algorithm.Stack.ArrayStack;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Stack;

public class Solution
{

    public boolean isValid2(String s){
        Stack<Character> stack = new Stack<>();
        int n = s.length();
        HashMap<Character,Character> hashMap = new HashMap<>();
        hashMap.put('(',')');
        hashMap.put('[',']');
        hashMap.put('{','}');

        for (int i = 0; i < n; i++) {
            if (hashMap.containsKey(s.charAt(i))){
                stack.push(hashMap.get(s.charAt(i)));
            }else {
                if (!stack.isEmpty() && stack.peek()==s.charAt(i)){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }
    public boolean isValid(String s) {

        int length = s.length();
        if (length%2!=0 || length==0)
        {
            return false;
        }

        HashMap<Character, Character> symbolHash = new HashMap<>();
        symbolHash.put('(',')');
        symbolHash.put('{','}');
        symbolHash.put('[',']');

        Deque<Character> stack = new LinkedList<>();

        for (int i = 0; i < length; i++)
        {
            char ch = s.charAt(i);
            if (symbolHash.containsKey(ch))
            {
                stack.push(symbolHash.get(ch));
            }else {
                if (stack.isEmpty() || ch!=stack.peek() )
                {
                    return false;
                }

                stack.pop();
            }
        }


        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String character= "(]";
        Solution solution = new Solution();
        boolean valid = solution.isValid(character);
        System.out.println(valid);
    }
}
